package ml.coursedesign.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ml.coursedesign.common.lang.Result;
import ml.coursedesign.common.lang.StatusCode;
import ml.coursedesign.entity.CourseSchedule;
import ml.coursedesign.service.CourseClassService;
import ml.coursedesign.service.CourseScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前端控制器：排课
 *
 * @author likeqc.top
 * @since 2021-04-27
 */
@RestController
@RequestMapping("/course-schedule")
public class CourseScheduleController {

  @Autowired CourseScheduleService courseScheduleService;

  @Autowired CourseClassService courseClassService;

  /**
   * 根据课程班级id查询该课程班级的日程安排
   *
   * @param id 课程班级id
   * @return
   */
  @GetMapping("/courseClass/{id}")
  public Result getSchedule(@PathVariable String id) {
    List<CourseSchedule> list =
        courseScheduleService.list(new QueryWrapper<CourseSchedule>().eq("course_class_id", id));

    return Result.succ(list);
  }

  /**
   * 查询教师的课程安排信息
   *
   * @param id 教师id
   * @return
   */
  @GetMapping("/teacher/{id}")
  public Result getScheduleByTeacherId(@PathVariable String id) {
    List<CourseSchedule> list =
        courseScheduleService.list(new QueryWrapper<CourseSchedule>().eq("teacher_id", id));
    return Result.succ(list);
  }

  /**
   * 添加一个课程安排
   *
   * @param courseSchedule 课程安排实体对象
   * @return
   */
  @PostMapping("/")
  public Result addschedule(@RequestBody CourseSchedule courseSchedule) {

    boolean save = courseScheduleService.save(courseSchedule);
    if (!save) {
      return Result.fail("添加课程安排失败");
    }
    return Result.succ(StatusCode.OK, "添加课程安排成功", null);
  }

  /**
   * 根据课程表ID删除指定的一次课程拿牌
   *
   * @param id 课程安排id
   * @return
   */
  @DeleteMapping("/{id}")
  public Result deleteById(@PathVariable String id) {
    courseScheduleService.removeById(id);
    return Result.succ(StatusCode.OK, "删除课程安排成功", null);
  }

  /**
   * 根据课程班级id删除该课程班级的所有课程安排
   *
   * @param id 课程班级id
   * @return
   */
  @DeleteMapping("/courseClass/{id}")
  public Result deleteByCourseClassId(@PathVariable String id) {
    courseScheduleService.remove(new QueryWrapper<CourseSchedule>().eq("course_class_id", id));
    return Result.succ(StatusCode.OK, "删除所有的课程安排成功", null);
  }
}
